import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


public class BusManager {
	private static BusManager instance = null;
	public static BusManager getInstance() throws SQLException {
		if (instance == null) {
			instance = new BusManager();
			return instance;
		} else {
			return instance;
		}
	}
	protected BusManager() throws SQLException {
		conn = Database.getAccessDBConnection();
	}
	Connection conn = null;
	
	public Bus getBus(int busId) {
		try {
			Statement stmt = conn.createStatement();
			stmt.execute("SELECT * FROM Bus" + " WHERE bus_id=" + busId);
			ResultSet rs = stmt.getResultSet();
			if (!rs.next()) {
				stmt.close();
				return null;
			} else {
				Bus b = new Bus(rs);
				stmt.close();
				return b;
			}
		} catch (SQLException e) {
			return null;
		}
	}
	
	public ArrayList<Bus> getBuses(int lineId) {
		ArrayList<Bus> bss = new ArrayList<Bus>();
		try {
			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM Bus"
					+ " WHERE " + "line_id = " + lineId;
			stmt.execute(sql);
			ResultSet rs = stmt.getResultSet();
			while (rs.next()) {
				int busId = rs.getInt("bus_id");
				Bus bl = getBus(busId);
				bss.add(bl);
			}
			stmt.close();
		} catch (SQLException e) {
		}
		return bss;
	}
}
